<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>数据劫持和观察者模式</title>
</head>
<body>
    
    <script>
        

        function Dep () {
            this.subs = [];
        }

        Dep.prototype.addSubs = function (sub) {
            this.subs.push(sub);
        }

        Dep.prototype.notify = function () {
            this.subs.forEach(sub=>{
                sub.update();
            })
        }

        function Watcher (fn) {
            this.fn = fn;
        }

        Watcher.prototype.update = function () {
            this.fn();
        }

        let w1 = new Watcher (function () {
            console.log('watcher1');
        });

        let w2 = new Watcher (function () {
            console.log('watcher2');
        });

        let dep = new Dep();

        dep.addSubs(w1);
        dep.addSubs(w2);

        dep.notify();

    
    </script>

</body>
</html>